# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit mount-boot

DESCRIPTION="The Xen virtual machine monitor and Xend daemon"
HOMEPAGE="http://xen.sourceforge.net"
SRC_URI="http://www.cl.cam.ac.uk/Research/SRG/netos/xen/downloads/${P}-src.tgz"
S="${WORKDIR}/${PN}-2.0"
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86"
IUSE="doc debug"

DEPEND="sys-apps/iproute2
	net-misc/bridge-utils
	dev-lang/python
	=dev-python/twisted-1.3.0
	net-misc/curl
	sys-libs/zlib
	doc? (
		dev-tex/latex2html
		media-gfx/transfig
	)"

src_compile() {
	local myopt
	if use debug; then
		myopt="${myopt} debug=y"
	fi
	emake ${myopt} -C xen || die "compiling xen failed"
	emake ${myopt} -C tools || die "compiling tools failed"
	if use doc; then
		sh ./docs/check_pkgs || die "package check failed"
		emake ${myopt} -C docs || die "compiling docs failed"
	fi

}

src_install() {
	make DESTDIR=${D} -C xen install || die "installing xen failed" 
	make DESTDIR=${D} XEN_PYTHON_NATIVE_INSTALL=1 -C tools install || die "installing tools failed" 
	if use doc; then
		make DESTDIR=${D} -C docs install \
			|| die "installing docs failed"
		# Rename doc/xen to the Gentoo-style doc/xen-2.0
		mv ${D}/usr/share/doc/{${PN},${PF}}
	fi
	
	# bind xend to localhost per default
	sed -i -e "s/\((xend-address  *\)'')/\1\'localhost\')/" \
		${D}/etc/xen/xend-config.sxp

	newinitd ${FILESDIR}/${PV}/xend-init xend
	newconfd ${FILESDIR}/${PV}/xend-conf xend
	newconfd ${FILESDIR}/${PV}/xendomains-conf xendomains
	newinitd ${FILESDIR}/${PV}/xendomains-init xendomains
}

pkg_postinst() {
	einfo ""
	einfo "The Xen virtual machine monitor (VMM) has been installed"
	einfo "  to /boot/xen.gz"
	einfo "The Xend daemon and control tools have also been installed."
	einfo ""
	einfo "To get a working Xen system, you still need to do a few things:"
	einfo "1: emerge xen-sources ; cd /usr/src/linux*xen0 ; make menuconfig ; make"
	einfo "     This downloads the linux kernel sources to /usr/src/ and"
	einfo "     applies the Xen patches. You will then need to configure"
	einfo "     and build a new dom0 kernel."
	einfo "2: Add the needed /boot/grub/grub.conf entries. You will end up"
	einfo "   with something that looks like:"
	einfo "     title=Xen / XenLinux"
	einfo "     root (hd0,0)"
	einfo "     kernel /xen.gz dom0_mem=131072"
	einfo "     module /initrd.gz"
	einfo ""
	einfo "Please visit the Xen homepage and read the manual:"
	einfo "http://www.cl.cam.ac.uk/Research/SRG/netos/xen/"
	einfo ""
}
